package com.runen.wnhz.runen.ui.activity.mine;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.common.utils.ACacheUtlis;
import com.runen.wnhz.runen.common.utils.JumpUtlis;
import com.runen.wnhz.runen.common.utils.StatusBarCompatUtils;
import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.ReceivingAddressEntity;
import com.runen.wnhz.runen.data.entity.UserBean;
import com.runen.wnhz.runen.di.component.ApplicationComponent;
import com.runen.wnhz.runen.di.component.DaggerAddressComponent;
import com.runen.wnhz.runen.di.module.AddressModule;
import com.runen.wnhz.runen.presenter.Contart.AddressContart;
import com.runen.wnhz.runen.presenter.iPresenter.IAddressPersenter;
import com.runen.wnhz.runen.ui.activity.BaseActivity;
import com.runen.wnhz.runen.ui.activity.login.LoginForPersonalActivity;
import com.runen.wnhz.runen.ui.activity.main.SubmitOrderActivity;
import com.runen.wnhz.runen.ui.adapter.puadapter.ReceivingAddressAdapter;
import com.runen.wnhz.runen.widget.TitleBuilder;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018-04-11.
 */

public class ReceivingAddressActivity extends BaseActivity<IAddressPersenter> implements AddressContart.View, View.OnClickListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.tv_add_address)
    TextView tv_add_address;

    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    private int page = 0;
    private int mType;
    private String mAid;

    private UserBean reqeustUser;

    private ReceivingAddressAdapter mAdapter;
    private int mDeletePosition;
    private List<ReceivingAddressEntity> mlist;
    private String moren;
    private boolean orderTag = false;

    @Override
    protected int getLayoutView() {
        return R.layout.receiving_address_activity_layout;
    }

    @Override
    protected void onTitleClickListen(TitleBuilder.TitleButton clicked) {
        switch (clicked) {
            case LEFT:
                finish();
                break;
            case MIDDLE:
                break;
            case RIGHT:
                break;
            default:
                break;
        }
    }

    @Override
    protected void initStart() {
        StatusBarCompatUtils.getInstance().TranslucentStatusBar(this, R.color.title_green);
        tv_add_address.setOnClickListener(this);
        reqeustUser = ACacheUtlis.getInstance().getReqeustUser(this);

        mSwipeRefreshLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mAdapter = new ReceivingAddressAdapter();
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.setPreLoadNumber(5);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.tv_edit:
                        mAid = mAdapter.getData().get(position).getAid();
                        Bundle bundle = new Bundle();
                        bundle.putInt("type", 2);
                        bundle.putSerializable("data", mAdapter.getData().get(position));
                        JumpUtlis.getInstance().jumpActivity(ReceivingAddressActivity.this
                                , AddAddressActivity.class, bundle);
                        // 返回刷新
                        page = 0;
                        break;
                    case R.id.tv_delete:
                        mDeletePosition = position;
                        mAid = mAdapter.getData().get(position).getAid();
                        mType = 3;
                        mPresenter.setAddress();
                        break;
                    case R.id.relative_receivePersonInfo:
                        orderTag = getIntent().getBooleanExtra("orderTag",false);
                        if(orderTag) {
                            if (mlist != null) {
                                Intent intent = new Intent(ReceivingAddressActivity.this, SubmitOrderActivity.class);
                                ReceivingAddressEntity receivingAddressEntity = mlist.get(position);
                                intent.putExtra("address", receivingAddressEntity);
                                setResult(Activity.RESULT_OK, intent);
                                finish();
                            }
                        }
                        break;
                }
            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getAddressList();
    }

    @Override
    public void initToolBar(TitleBuilder mTitleBuilder) {
        mTitleBuilder.setMiddleTitleText("收货地址")
                .setLeftDrawable(R.mipmap.icon_back)
                .seTitleBgRes(R.mipmap.bg_title)
                .setMiddleTitleTextColor(ContextCompat.getColor(this, R.color.text_white));
    }

    @Override
    public void setupAcitivtyComponent(ApplicationComponent appComponent) {
        DaggerAddressComponent.builder()
                .applicationComponent(appComponent)
                .addressModule(new AddressModule(this))
                .build()
                .initJect(this);
    }

    @Override
    public void onTokenError(String msg) {
        super.onTokenError(msg);
        startActivity(new Intent(ReceivingAddressActivity.this, LoginForPersonalActivity.class));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_add_address:
                Bundle bundle = new Bundle();
                bundle.putInt("type", 1);
                JumpUtlis.getInstance().jumpActivity(this, AddAddressActivity.class, bundle);
                break;
            default:
                break;
        }
    }

    @Override
    public void showLoadding() {

    }

    @Override
    public void dimissLoding() {

    }

    @Override
    public void showErrorMessage(String msg) {
        mSwipeRefreshLayout.setRefreshing(false);
        ToastUtil.showToast(msg);
    }

    @Override
    public String getToken() {
        return reqeustUser.getToken();
    }

    @Override
    public String getPage() {
        return String.valueOf(page);
    }

    @Override
    public void getReceivingRecycler(List<ReceivingAddressEntity> mlist) {
        this.mlist = mlist;
        mSwipeRefreshLayout.setRefreshing(false);
        setData(page == 0, mlist);
        page++;
    }

    @Override
    public String getType() {
        return mType + "";
    }

    @Override
    public String getAid() {
        return mAid;
    }

    @Override
    public String getName() {
        return null;
    }

    @Override
    public String getPhone() {
        return null;
    }

    @Override
    public String getprovince() {
        return null;
    }

    @Override
    public String getCity() {
        return null;
    }

    @Override
    public String getCounty() {
        return null;
    }

    @Override
    public String getAddress() {
        return null;
    }

    @Override
    public String getIsDefault() {
        return null;
    }

    @Override
    public void onSuccess(String info) {
        if (mType == 3){
            mAdapter.getData().remove(mDeletePosition);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onStop() {
        if(mlist != null){
           editor.putString("default_address",new Gson().toJson(mlist.get(0))).commit();
        }
        super.onStop();
    }

    @Override
    public void onRefresh() {
        page = 0;
        mPresenter.getAddressList();
    }

    @Override
    public void onLoadMoreRequested() {
        mRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                mPresenter.getAddressList();
            }
        });
    }

    private void setData(boolean isRefresh, List data) {
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < 10) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }
}
